package com.linln.admin.util;

import java.net.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class NetworkInfoUtil {

    // 获取所有有效的网络接口信息
    public static List<NetworkInterfaceInfo> getNetworkInterfaces() throws SocketException {
        List<NetworkInterfaceInfo> interfaces = new ArrayList<>();
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();

        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            try {
                // 过滤掉未启用、回环、虚拟或未连接的接口
                if (networkInterface.isLoopback() || !networkInterface.isUp() || networkInterface.isVirtual()) {
                    continue;
                }

                // 获取IPv4地址
                String ip = getIpv4Address(networkInterface);
                if (ip == null) {
                    continue;
                }

                // 获取MAC地址并格式化
                String mac = formatMacAddress(networkInterface.getHardwareAddress());
                if (mac == null) {
                    continue;
                }

                interfaces.add(new NetworkInterfaceInfo(
                        networkInterface.getName(),
                        networkInterface.getDisplayName(),
                        ip,
                        mac
                ));
            } catch (SocketException e) {
                // 处理异常
            }
        }
        return interfaces;
    }

    // 获取IPv4地址
    private static String getIpv4Address(NetworkInterface networkInterface) {
        Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
        while (inetAddresses.hasMoreElements()) {
            InetAddress inetAddress = inetAddresses.nextElement();
            if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
                return inetAddress.getHostAddress();
            }
        }
        return null;
    }

    // 格式化MAC地址
    private static String formatMacAddress(byte[] mac) {
        if (mac == null || mac.length == 0) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
        }
        return sb.toString();
    }

    // 网络接口信息封装类
    public static class NetworkInterfaceInfo {
        private final String name;
        private final String displayName;
        private final String ip;
        private final String mac;

        public NetworkInterfaceInfo(String name, String displayName, String ip, String mac) {
            this.name = name;
            this.displayName = displayName;
            this.ip = ip;
            this.mac = mac;
        }

        // Getters
        public String getName() { return name; }
        public String getDisplayName() { return displayName; }
        public String getIp() { return ip; }
        public String getMac() { return mac; }
    }
}
